package com.github.browep.bat.activities;

import android.content.res.Resources;
import android.os.Bundle;
import android.text.util.Linkify;
import android.util.Log;
import android.widget.TextView;
import com.github.browep.bat.R;

import java.io.IOException;
import java.io.InputStream;

import static com.github.browep.bat.Constants.SCREEN_ID;

/**
 * Created by IntelliJ IDEA.
 * User: paul
 * Date: 8/28/11
 * Time: 11:02 AM
 * To change this template use File | Settings | File Templates.
 */
public class Html extends BaseActivity {
    public static final String LOG_TAG = Html.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupWrappedUI(R.layout.html);
        int id = getResources().getIdentifier((String) getIntent().getExtras().get(SCREEN_ID), "raw", getPackageName());
        InputStream inputStream;

        try {
            inputStream = getResources().openRawResource(id);
        } catch (Resources.NotFoundException e) {
            id = getResources().getIdentifier("notfound", "raw", getPackageName());
            inputStream = getResources().openRawResource(id);
        }
        TextView blurbView = (TextView) findViewById(R.id.html);
        try {
            blurbView.setText(android.text.Html.fromHtml(slurp(inputStream)));
            Linkify.addLinks(blurbView, Linkify.ALL);
        } catch (IOException e) {
            Log.e(LOG_TAG,"error slurping input stream in html",e);
        }


    }

    public static String slurp(InputStream in) throws IOException {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = in.read(b)) != -1; ) {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }
}
